#include "EquipDetail.h"
#include "../../model/proxy/ItemProxy.h"
#include "../../model/static/StaticItem.h"
#include "EquipDetailLoader.h"

EquipDetail::EquipDetail(void)
	:mName(NULL),mLevel(NULL),mType(NULL),mPropertyTitle(NULL),mPropertyValue(NULL)
	,mAffix1Title(NULL),mAffix1Value(NULL),mAffix2Title(NULL),mAffix2Value(NULL),
	mAffix3Title(NULL),mAffix3Value(NULL),mAffix4Title(NULL),mAffix4Value(NULL)
{
}

EquipDetail::~EquipDetail(void)
{
	CC_SAFE_RELEASE(mName);CC_SAFE_RELEASE(mLevel);
	CC_SAFE_RELEASE(mType);CC_SAFE_RELEASE(mPropertyTitle);
	CC_SAFE_RELEASE(mPropertyValue);CC_SAFE_RELEASE(mAffix1Title);
	CC_SAFE_RELEASE(mAffix1Value);CC_SAFE_RELEASE(mAffix2Title);
	CC_SAFE_RELEASE(mAffix2Value);CC_SAFE_RELEASE(mAffix3Title);
	CC_SAFE_RELEASE(mAffix3Value);CC_SAFE_RELEASE(mAffix4Title);
	CC_SAFE_RELEASE(mAffix4Value);
}

bool EquipDetail::onAssignCCBMemberVariable( CCObject * pTarget, CCString * pMemberVariableName, CCNode * pNode )
{
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mName", CCLabelTTF *, this->mName);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mLevel", CCLabelTTF *, this->mLevel);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mType", CCLabelTTF *, this->mType);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mPropertyTitle", CCLabelTTF *, this->mPropertyTitle);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mPropertyValue", CCLabelTTF *, this->mPropertyValue);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAffixTitle1", CCLabelTTF *, this->mAffix1Title);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAffixValue1", CCLabelTTF *, this->mAffix1Value);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAffixTitle2", CCLabelTTF *, this->mAffix2Title);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAffixValue2", CCLabelTTF *, this->mAffix2Value);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAffixTitle3", CCLabelTTF *, this->mAffix3Title);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAffixValue3", CCLabelTTF *, this->mAffix3Value);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAffixTitle4", CCLabelTTF *, this->mAffix4Title);
	CCB_MEMBERVARIABLEASSIGNER_GLUE(this, "mAffixValue4", CCLabelTTF *, this->mAffix4Value);
	return false;
}

SEL_MenuHandler EquipDetail::onResolveCCBCCMenuItemSelector(CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCMENUITEM_GLUE(this, "onDoItClick", EquipDetail::onDoItClick);
	return NULL;
}

SEL_CCControlHandler EquipDetail::onResolveCCBCCControlSelector( CCObject * pTarget, CCString * pSelectorName )
{
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onSnatchBtnClick", EquipDetail::onSnatchBtnClick);
	CCB_SELECTORRESOLVER_CCCONTROL_GLUE(this, "onCloseBtnClick", EquipDetail::onCloseBtnClick);
	return NULL;
}

void EquipDetail::onNodeLoaded( CCNode * pNode, CCNodeLoader * pNodeLoader )
{
	AffixView* affixView;
	affixView = new AffixView();
	affixView->mAffixTitle = mAffix1Title;
	affixView->mAffixValue = mAffix1Value;
	mAffixView.push_back(affixView);
	affixView = new AffixView();
	affixView->mAffixTitle = mAffix2Title;
	affixView->mAffixValue = mAffix2Value;
	mAffixView.push_back(affixView);
	affixView = new AffixView();
	affixView->mAffixTitle = mAffix3Title;
	affixView->mAffixValue = mAffix3Value;
	mAffixView.push_back(affixView);
	affixView = new AffixView();
	affixView->mAffixTitle = mAffix4Title;
	affixView->mAffixValue = mAffix4Value;
	mAffixView.push_back(affixView);
	mEquipIcon = EquipIcon::create(this);
	mEquipIcon->setScale(0.4);
	addChild(mEquipIcon);
}

bool EquipDetail::init()
{
	return true;
}

void EquipDetail::onEnter()
{
	CCNode::onEnter();
}

void EquipDetail::onExit()
{
	CCNode::onExit();
}

void EquipDetail::refresh()
{
	EquipInfo* equipInfo = (EquipInfo*)mData;
	EquipStatic* equipStatic = StaticItem::getSingletonPtr()->getEquipInfo(equipInfo->id);
	mName->setString(equipStatic->name);
	mLevel->setString(fcs("lv %d", equipInfo->level));
	mType->setString(StaticItem::getSingletonPtr()->getTypeTitle(equipInfo->id));
	mPropertyTitle->setString(StaticItem::getSingletonPtr()->getPropertyTitle(equipInfo->id));
	std::string propertyValue;
	ItemProxy::getSingletonPtr()->getPropertyValue(equipInfo, propertyValue);
	mPropertyValue->setString(propertyValue.c_str());
	AffixList& affixList = equipInfo->affixs;
	for (int i = 0; i < mAffixView.size(); i++)
	{
		mAffixView[i]->mAffixTitle->setVisible(false);
		mAffixView[i]->mAffixValue->setVisible(false);
	}
	for (int i = 0; i < affixList.size(); i++)
	{
		Affix* affix = affixList[i];
		std::string title;
		ItemProxy::getSingletonPtr()->getAffixTitle(affix, title);
		mAffixView[i]->mAffixTitle->setString(title.c_str());
		mAffixView[i]->mAffixValue->setString(fcs("+%d%%", affix->value));
		mAffixView[i]->mAffixTitle->setVisible(true);
		mAffixView[i]->mAffixValue->setVisible(true);
	}
	
	mEquipIcon->setInfo(equipInfo->id);
}

void EquipDetail::_onNotification( CCObject* object )
{
	
}

void EquipDetail::onDoItClick(CCObject* pSender)
{
    
}

void EquipDetail::onSnatchBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{

}

void EquipDetail::onCloseBtnClick( CCObject * pSender, CCControlEvent pCCControlEvent )
{

}

EquipDetail* EquipDetail::create( CCObject* pOwer )
{
	RegisterLoader("EquipDetail", EquipDetailLoader::loader());
	return (EquipDetail*)(ReadNode("EquipDetail.ccbi", pOwer));
}
